package cn.anecansaitin.firecrafting.client.screen;

import cn.anecansaitin.firecrafting.ModConstant;
import cn.anecansaitin.firecrafting.client.util.RenderHelper;
import cn.anecansaitin.firecrafting.common.block.FireInterface;
import cn.anecansaitin.firecrafting.common.block.entity.FireInterfaceEntity;
import cn.anecansaitin.firecrafting.common.menu.FireInterfaceMenu;
import cn.anecansaitin.firecrafting.common.net.ModNetwork;
import cn.anecansaitin.firecrafting.common.net.pack.ScreenButtonPressedToBlock;
import cn.anecansaitin.firecrafting.common.net.pack.ScreenButtonPressedToMenu;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.gui.widget.ExtendedButton;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class FireInterfaceScreen extends AbstractContainerScreen<FireInterfaceMenu> {
    private static final ResourceLocation gui = new ResourceLocation(ModConstant.MOD_ID, "textures/gui/fire_interface.png");
    private final FireInterfaceEntity blockEntity;
    private boolean filterMode;

    public FireInterfaceScreen(FireInterfaceMenu menu, Inventory playerInv, Component title) {
        super(menu, playerInv, title);
        blockEntity = menu.getBlockEntity();
        filterMode = blockEntity.isFilterMode();
        imageWidth = 190;
        imageHeight = 192;
    }

    @Override
    protected void init() {
        super.init();
        addRenderableWidget(new ExtendedButton(getGuiLeft() + 174, getGuiTop() + 74, 15, 15, new TextComponent(""), (button) -> {
            ModNetwork.CHANNEL.sendToServer(new ScreenButtonPressedToMenu(0));
            menu.clickMenuButton(minecraft.player, 0);
        }));
        addRenderableWidget(new ExtendedButton(getGuiLeft() + 174, getGuiTop() + 90, 15, 15, new TextComponent(""), (button) -> {
            ModNetwork.CHANNEL.sendToServer(new ScreenButtonPressedToBlock(1, blockEntity.getBlockPos()));
            filterMode = !filterMode;
            blockEntity.switchFilterMode();
        }));
    }

    @Override
    public void render(@NotNull PoseStack poseStack, int mouseX, int mouseY, float partialTick) {
        super.render(poseStack, mouseX, mouseY, partialTick);
        RenderSystem.setShaderTexture(0, gui);
        RenderSystem.setShaderColor(1, 1, 1, 1);
        //过滤界面按钮
        if (menu.isFilter()) {
            blit(poseStack, getGuiLeft() + 174, getGuiTop() + 74, 218, 74, 15, 15);
        } else {
            blit(poseStack, getGuiLeft() + 175, getGuiTop() + 75, 204, 75, 13, 13);
        }
        //过滤模式按钮
        if (filterMode) {
            blit(poseStack, getGuiLeft() + 174, getGuiTop() + 90, 218, 90, 15, 15);
        } else {
            blit(poseStack, getGuiLeft() + 175, getGuiTop() + 91, 204, 91, 13, 13);
        }
        drawFluidTip(mouseX, mouseY, getGuiLeft() + 173, getGuiTop() + 11, 18, 61);
        drawEnergyTip(mouseX, mouseY, getGuiLeft() + 191, getGuiTop() + 11, 6, 61);
        renderTooltip(poseStack, mouseX, mouseY);
    }

    @Override
    protected void renderBg(@NotNull PoseStack poseStack, float partialTick, int mouseX, int mouseY) {
        renderBackground(poseStack);
        RenderSystem.setShaderTexture(0, gui);
        RenderSystem.disableDepthTest();
        RenderSystem.setShaderColor(1, 1, 1, 1);
        blit(poseStack, getGuiLeft(), getGuiTop(), 0, 0, 176, 193);
        blit(poseStack, getGuiLeft() + 173, getGuiTop() + 8, 173, 8, 27, 102);
        //过滤界面蓝色格子
        if (menu.isFilter()) {
            blit(poseStack, getGuiLeft() + 7, getGuiTop() + 17, 7, 193, 162, 45);
        }
        //过滤模式显示过滤物品
        if (filterMode && !menu.isFilter()) {
            ItemStackHandler filter = blockEntity.getFilter();
            for (int i = 0; i < filter.getSlots(); i++) {
                ItemStack stack = filter.getStackInSlot(i);
                if (!stack.isEmpty()) {
                    Slot slot = menu.getSlot(i + 36);
                    itemRenderer.renderGuiItem(stack, getGuiLeft() + slot.x, getGuiTop() + slot.y);
                }
            }
            //额外绘制一层半透明的灰色，以淡化物品
            RenderSystem.setShaderColor(1, 1, 1, 0.5f);
            RenderSystem.setShaderTexture(0, gui);
            RenderSystem.enableBlend();
            poseStack.pushPose();
            poseStack.translate(0, 0, 110);
            blit(poseStack, getGuiLeft() + 7, getGuiTop() + 17, 7, 17, 162, 45);
            poseStack.popPose();
        }
        RenderSystem.disableBlend();
        RenderSystem.setShaderColor(1, 1, 1, 1);
        //画中间的小火焰
        if (blockEntity.getBlockState().getValue(FireInterface.LIT)) {
            blit(poseStack, getGuiLeft() + 81, getGuiTop() + 64, 233, 64, 14, 14);
        }
        //计算流体的长度
        int fluidBar = Math.floorDiv(blockEntity.getFluidOut().getFluidAmount() * 58, blockEntity.getFluidOut().getCapacity());
        //画流体
        RenderHelper.drawFluid(getGuiLeft() + 174, getGuiTop() + 71 - fluidBar, blockEntity.getFluidOut().getFluid(), 16, fluidBar);
        //画流体上的刻度
        RenderSystem.setShaderTexture(0, gui);
        RenderSystem.setShaderColor(1, 1, 1, 1);
        blit(poseStack, getGuiLeft() + 174, getGuiTop() + 13, 203, 13, 16, 58);
        //计算能量的长度
        int energyBar = Math.floorDiv(blockEntity.getEnergyOut().getEnergyStored() * 58, blockEntity.getEnergyOut().getMaxEnergyStored());
        //画能量
        blit(poseStack, getGuiLeft() + 193, getGuiTop() + 71 - energyBar, 219, 70 - energyBar, 3, energyBar);
    }

    //文字的渲染
    @Override
    protected void renderLabels(@NotNull PoseStack poseStack, int mouseX, int museY) {
        font.draw(poseStack, title, titleLabelX, titleLabelY, 4210752);
        font.draw(poseStack, playerInventoryTitle, inventoryLabelX, inventoryLabelY + 28, 4210752);
    }

    private void drawFluidTip(int mouseX, int mouseY, int x, int y, int width, int height) {
        if (isMouseover(mouseX, mouseY, x - 1, y - 1, width + 1, height + 1)) {
            FluidStack fluidStack = blockEntity.getFluidOut().getFluid();
            Component tip = new TranslatableComponent("tooltip.fire_crafting.fluid").append(new TranslatableComponent("tooltip.fire_crafting.fluid_empty"));
            if (!fluidStack.isEmpty()) {
                Component count = new TextComponent(fluidStack.getAmount() + "/" + blockEntity.getFluidOut().getCapacity() + " ").withStyle(ChatFormatting.BLUE);
                tip = new TranslatableComponent("tooltip.fire_crafting.fluid", count).append(fluidStack.getDisplayName());
            }
            ArrayList<Component> list = new ArrayList<>();
            list.add(tip);
            this.renderComponentTooltip(new PoseStack(), list, mouseX, mouseY);
        }
    }

    private void drawEnergyTip(int mouseX, int mouseY, int x, int y, int width, int height) {
        if (isMouseover(mouseX, mouseY, x, y, width, height)) {
            ArrayList<Component> list = new ArrayList<>();
            list.add(new TranslatableComponent("tooltip.fire_crafting.energy").append(new TranslatableComponent(blockEntity.getEnergyOut().getEnergyStored() + " FE").withStyle(ChatFormatting.YELLOW)));
            renderComponentTooltip(new PoseStack(), list, mouseX, mouseY);
        }
    }

    public boolean isMouseover(int mouseX, int mouseY, int x, int y, int width, int height) {
        return mouseX > x && x + width > mouseX && mouseY > y && y + height > mouseY;
    }
}
